package com.ruoyi.web.controller.content;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shop.common.utils.SnowflakeIdWorker;
import com.ruoyi.shop.modules.domain.Attachment;
import com.ruoyi.shop.modules.service.IAttachmentService;
import com.ruoyi.shop.modules.service.IMinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件Controller
 *
 * @author 朱津辰
 * @date 2024-05-22
 */
@RestController
@RequestMapping("/shop/attachment")
public class AttachmentController extends BaseController {
    @Autowired
    private IAttachmentService attachmentService;
    @Autowired
    private IMinioService minioService;
    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);


    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Attachment attachment) {
        startPage();
        List<Attachment> list = attachmentService.selectAttachmentList(attachment);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attachment attachment) {
        List<Attachment> list = attachmentService.selectAttachmentList(attachment);
        ExcelUtil<Attachment> util = new ExcelUtil<Attachment>(Attachment.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(attachmentService.selectAttachmentById(id));
    }


    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attachment attachment) {
        return toAjax(attachmentService.insertAttachment(attachment));
    }


    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attachment attachment) {
        return toAjax(attachmentService.updateAttachment(attachment));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('shop:attachment:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(attachmentService.deleteAttachmentByIds(ids));
    }
}
